﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKPatternSign.StairsClearHeightDimension
{
	internal class StairsClearHeightDimensionGeometry
	{
		public static XYZ CalculatMidPoint(List<XYZ> arPoint)
		{
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			foreach (XYZ xyz in arPoint)
			{
				num += xyz.X;
				num2 += xyz.Y;
				num3 += xyz.Z;
			}
			int count = arPoint.Count;
			return new XYZ(num / (double)count, num2 / (double)count, num3 / (double)count);
		}

		public static List<XYZ> SortPointsWithVector(List<XYZ> srcPoints, XYZ vecDirection)
		{
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			list.AddRange(srcPoints);
			SortedList<double, List<XYZ>> sortedList = new SortedList<double, List<XYZ>>();
			XYZ xyz = vecDirection.Multiply(1.0);
			foreach (XYZ xyz2 in srcPoints)
			{
				double d = xyz.AngleTo(xyz2);
				double key = Math.Sqrt(xyz2.X * xyz2.X + xyz2.Y * xyz2.Y + xyz2.Z * xyz2.Z) * Math.Cos(d);
				if (!sortedList.ContainsKey(key))
				{
					sortedList.Add(key, new List<XYZ>
					{
						xyz2
					});
				}
				else
				{
					sortedList[key].Add(xyz2);
				}
			}
			if (sortedList.Count > 0)
			{
				IEnumerator<KeyValuePair<double, List<XYZ>>> enumerator2 = sortedList.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					List<XYZ> list3 = list2;
					KeyValuePair<double, List<XYZ>> keyValuePair = enumerator2.Current;
					list3.AddRange(keyValuePair.Value);
				}
				if (list2.Count == srcPoints.Count)
				{
					list = list2;
				}
			}
			return list;
		}
	}
}
